﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HHCommon;
namespace HHWeb.SystemManage
{
    public partial class Login : System.Web.UI.Page
    {
        Help help = new Help();
        EncAndDec encanddec = new EncAndDec();//加密和解密类
        NoHtml nohtml = new NoHtml();//文本非法字符过滤类
        HHBLL.Users user = new HHBLL.Users();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (help.GetHttpMethodBool())
            {
                string UserCode = Request["UserCode"].ToString();
                string UserPassWord = Request["UserPassWord"].ToString();
                if (string.IsNullOrEmpty(UserCode))
                {
                    MessageBox.Show("登录账号不能为空！");
                    return;
                }
                if (string.IsNullOrEmpty(UserPassWord))
                {
                    MessageBox.Show("登录密码不能为空！");
                    return;
                }
                UserCode = nohtml.NoHTML(UserCode);
                UserPassWord = help.GetMD5(nohtml.NoHTML(UserPassWord));
                HHModel.Users UserModel = user.GetModel(UserCode, UserPassWord);
                if (UserModel != null)
                {
                    //if (UserModel.RegState == 1)
                    //{
                    //    MessageBox.Show("该账户已经登录！");
                    //    return;
                    //}
                    //user.UpdateRegStateByUserNumber(UserModel.UserNumber, 1);
                    //设置Cookie保存登录信息
                    help.SetCookie("ID", encanddec.DESEncrypting(UserModel.ID.ToString()), SetCookieTime.Hours,0);
                    help.SetCookie("UserNumber", encanddec.DESEncrypting(UserModel.UserNumber), SetCookieTime.Hours,0);
                    help.SetCookie("UserName", encanddec.DESEncrypting(UserModel.UserName), SetCookieTime.Hours,0);
                    help.SetCookie("UserType", encanddec.DESEncrypting(UserModel.UserType), SetCookieTime.Hours,0);
                    help.SetCookie("UserCode", encanddec.DESEncrypting(UserModel.UserCode), SetCookieTime.Hours,0);
                    MessageBox.Show("登录成功！", "main.aspx");
                }
                else
                {
                    MessageBox.Show("登录账号或密码错误！");
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(help.GetCookie("UserNumber")) && !string.IsNullOrEmpty(help.GetCookie("UserCode")))
                {
                    Response.Redirect("main.aspx");
                    return;
                }
            }
        }
    } 
}